package com.augrain.microframework.web.validation.validator;

import com.augrain.microframework.core.util.StringUtils;
import com.augrain.microframework.web.validation.annotation.IsPhoneNumber;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 手机号校验
 *
 * @author biaoy
 * @since 2025/01/21
 */
public class PhoneNumberValidator implements ConstraintValidator<IsPhoneNumber, String> {

    private static final String PHONE_REG = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";

    @Override
    public boolean isValid(String phoneNumber, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(phoneNumber)) {
            return false;
        }

        return phoneNumber.matches(PHONE_REG);
    }
}